package com.sgl.gk.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.utils.*;
import com.sgl.gk.controller.vo.*;
import com.sgl.gk.domain.*;
import com.sgl.gk.mapper.*;
import com.sgl.gk.service.GkStationConfigurationConnectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author:xec
 * @date:2022/11/11 9:14
 **/
@Service
@Slf4j
public class GkStationConfigurationConnectServiceImpl extends BaseController implements GkStationConfigurationConnectService {

    @Autowired
    GkStationConfigurationConnectMapper gkStationConfigurationConnectMapper;

    @Autowired
    GkStationConfigureTemplateMapper gkStationConfigureTemplateMapper;

    @Autowired
    GkStationConfigureTemplateInfoMapper gkStationConfigureTemplateInfoMapper;

    @Autowired
    GkStationConfigurationValuesMapper gkStationConfigurationValuesMapper;

    @Autowired
    GkStationEventMapper gkStationEventMapper;

    @Autowired
    GkStationConfigurationMapper gkStationConfigurationMapper;

    @Autowired
    GkStationMapper gkStationMapper;

    @Autowired
    GkSettingMapper gkSettingMapper;

    @Autowired
    GkProductMapper gkProductMapper;

    @Autowired
    GkProductionLineMapper gkProductionLineMapper;

    @Autowired
    GkDataValueLogMapper gkDataValueLogMapper;

    public Thread thread = null;

    @Override
    public List<DataTraceabilityVo> list(GkStationConfigurationConnect gkStationConfigurationConnect) {
        if (gkStationConfigurationConnect.getBarCode() == null && gkStationConfigurationConnect.getStartTime() == null && gkStationConfigurationConnect.getEndTime() == null) {
            return new ArrayList<>();
        }
        List<DataTraceabilityVo> list = new ArrayList<>();
        if (gkStationConfigurationConnect.getBarCode() != null) {
            List<GkStationConfigurationValues> stationConfigurationValues = gkStationConfigurationValuesMapper.selectByValue(gkStationConfigurationConnect.getBarCode());
            if (CollUtil.isEmpty(stationConfigurationValues)){
                stationConfigurationValues = new ArrayList<>();
                List<GkDataValueLog> gkDataValueLogList = new ArrayList<>();
                if (gkStationConfigurationConnect.getStartTime()!=null&&gkStationConfigurationConnect.getEndTime()!=null){
                    gkDataValueLogList = gkDataValueLogMapper.selectByTime(gkStationConfigurationConnect.getStartTime(),gkStationConfigurationConnect.getEndTime());
                }else {
                    // 近三个月的数据
                    gkDataValueLogList = gkDataValueLogMapper.selectLimit14();
                }
                if (CollUtil.isNotEmpty(gkDataValueLogList)) {
                    for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByValueAndByTableName(gkDataValueLog.getSurfaceName(), gkStationConfigurationConnect.getBarCode());
                        if (CollUtil.isNotEmpty(gkStationConfigurationValues)) {
                            stationConfigurationValues.addAll(gkStationConfigurationValues);
                        }
                    }
                }
            }
            if (stationConfigurationValues == null || stationConfigurationValues.size() <= 0) {
                return new ArrayList<>();
            }
            List<Long> collect = stationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationConnectId).collect(Collectors.toList());
            Long[] longs = new Long[collect.size()];
            for (int i = 0; i < collect.size(); i++) {
                longs[i] = collect.get(i);
            }
            if (stationConfigurationValues != null) {
                if (ServletUtils.getParameter("pageSize") != null && ServletUtils.getParameter("pageNum") != null) {
                    startPage();
                }
                GkStationConfigurationConnect configurationConnect = new GkStationConfigurationConnect();
                if (gkStationConfigurationConnect.getStartTime() != null && gkStationConfigurationConnect.getEndTime() != null) {
                    configurationConnect.setStartTime(gkStationConfigurationConnect.getStartTime());
                    configurationConnect.setEndTime(gkStationConfigurationConnect.getEndTime());
                    list = gkStationConfigurationConnectMapper.selectInfoByIdsAndTime(longs, configurationConnect);
                } else {
                    list = gkStationConfigurationConnectMapper.selectInfoByIdsShowVoOrderByLastModify(longs);
                }
            }
        } else {
            if (ServletUtils.getParameter("pageSize") != null && ServletUtils.getParameter("pageNum") != null) {
                startPage();
            }
            list = gkStationConfigurationConnectMapper.list(gkStationConfigurationConnect);
        }
        return list;
    }


    @Override
    public AjaxResult selectInfosById(Long id) {
        // 得到id所在的连接表值
        GkStationConfigurationConnect stationConfigurationConnect = gkStationConfigurationConnectMapper.selectInfoById(id);
        if (stationConfigurationConnect == null) {
            return AjaxResult.success();
        }
        // 根据模板名称查询到相对应的模板信息
        String productName = stationConfigurationConnect.getProductName();
        if (StringUtils.isEmpty(productName)){
            return AjaxResult.success();
        }
        GkProduct gkProduct = new GkProduct();
        gkProduct.setName(productName);
        List<GkProduct> list = gkProductMapper.list(gkProduct);
        List<String> tempNames = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            tempNames = list.stream().map(GkProduct::getTemplateName).collect(Collectors.toList());
        }
        List<GkStationConfigureTemplate> gkStationConfigureTemplates = new ArrayList<>();
        List<Long> tempIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(tempNames)) {
            gkStationConfigureTemplates = gkStationConfigureTemplateMapper.selectInfoByNames(tempNames);
            tempIds = gkStationConfigureTemplates.stream().map(GkStationConfigureTemplate::getId).collect(Collectors.toList());
        }
        Map<Long, List<GkStationConfiguration>> configByStationIdMap = new HashMap<>();
        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectId(id);
        if (CollUtil.isEmpty(gkStationConfigurationValues)){
            gkStationConfigurationValues = new ArrayList<>();
            List<GkDataValueLog> gkDataValueLogList = gkDataValueLogMapper.selectAll();
            if (CollUtil.isNotEmpty(gkDataValueLogList)) {
                Date lastModifyAt = stationConfigurationConnect.getLastModifyAt();
                for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                    if (lastModifyAt.compareTo(gkDataValueLog.getDataStartTime())>=0&& lastModifyAt.compareTo(gkDataValueLog.getDataEndTime())<=0) {
                        List<GkStationConfigurationValues> stationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectIdByTableName(gkDataValueLog.getSurfaceName(), id);
                        if (CollUtil.isNotEmpty(stationConfigurationValues)) {
                            gkStationConfigurationValues.addAll(stationConfigurationValues);
                        }
                    }
                }
            }
        }
        if (CollUtil.isNotEmpty(tempIds)) {
            List<GkStationConfigureTemplateInfo> gkStationConfigureTemplateInfos = gkStationConfigureTemplateInfoMapper.selectInfoByConfigureTemplateIds(tempIds);
            List<Long> configIds = gkStationConfigureTemplateInfos.stream().map(GkStationConfigureTemplateInfo::getStationConfigurationId).collect(Collectors.toList());
            List<GkStationConfiguration> configInfos = gkStationConfigurationMapper.selectInfoByIds(configIds);
            for (GkStationConfiguration configInfo : configInfos) {
                for (GkStationConfigureTemplateInfo gkStationConfigureTemplateInfo : gkStationConfigureTemplateInfos) {
                    if (gkStationConfigureTemplateInfo.getStationConfigurationId().longValue() == configInfo.getId().longValue()) {
                        configInfo.setTempToUse(gkStationConfigureTemplateInfo.getTempToUse());
                        if (StringUtils.isNotEmpty(gkStationConfigureTemplateInfo.getTempConfigName())){
                            configInfo.setDisplayName(gkStationConfigureTemplateInfo.getTempConfigName());
                        }
                    }
                }
            }
            configInfos = configInfos.stream().filter(configInfo -> configInfo.getType() == 2).collect(Collectors.toList());
            configByStationIdMap = configInfos.parallelStream().collect(Collectors.groupingBy(GkStationConfiguration::getStationId));
        }

        if (CollUtil.isNotEmpty(gkStationConfigurationValues)) {
            // 工位事件集合，得到所需要读取的工位
            List<Long> configId = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationId).distinct().collect(Collectors.toList());
            List<GkStationConfiguration> gkStationConfigurationList = gkStationConfigurationMapper.selectInfoByIds(configId);
            List<Long> stationIds = gkStationConfigurationList.stream().map(GkStationConfiguration::getStationId).distinct().collect(Collectors.toList());
            List<GkStation> stationList = gkStationMapper.selectInfosByIds(stationIds);
            List<Long> productionLineIds = stationList.stream().map(GkStation::getProductionLineId).collect(Collectors.toList());
            List<GkProductionLineVo> gkProductionLineVos = gkProductionLineMapper.selectInfoByIds(productionLineIds);

            Map<Long, List<GkStation>> stationByProductLineMap = stationList.parallelStream().collect(Collectors.groupingBy(GkStation::getProductionLineId));
            Map<Long, List<GkStationConfiguration>> configByStationMap = gkStationConfigurationList.parallelStream().collect(Collectors.groupingBy(GkStationConfiguration::getStationId));

            List<Map<String, Object>> mapList = new ArrayList<>();
            for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                Map<String, Object> map = new HashMap<>();
                map.put("gkProductionLineId", gkProductionLineVo.getId());
                map.put("gkProductionLineName", gkProductionLineVo.getName());
                List<GkStation> gkStations = stationByProductLineMap.get(gkProductionLineVo.getId());
                if (CollUtil.isNotEmpty(gkStations)) {
                    List<DataTraceabilityInfosVo> dataTraceabilityInfosVos = new ArrayList<>();
                    for (GkStation gkStation : gkStations) {
                        DataTraceabilityInfosVo dataTraceabilityInfosVo = new DataTraceabilityInfosVo();
                        dataTraceabilityInfosVo.setStationId(gkStation.getId());
                        dataTraceabilityInfosVo.setStationName(gkStation.getName());
                        dataTraceabilityInfosVo.setCreateAt(stationConfigurationConnect.getCreateAt());
                        List<GkStationConfigurationVo> gkStationConfigurationVoList = new ArrayList<>();
                        List<GkStationConfigurationVo> gkStationConfigurationVos = new ArrayList<>();
                        List<GkStationConfiguration> gkStationConfigurations = configByStationMap.get(gkStation.getId());
                        if (CollUtil.isNotEmpty(configByStationIdMap)) {
                            List<GkStationConfiguration> gkStationConfigurationList1 = configByStationIdMap.get(gkStation.getId());
                            if (CollUtil.isNotEmpty(gkStationConfigurationList1)) {
                                // 把模板使用赋值给相对应的工位信息上
                                for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
                                    for (GkStationConfiguration stationConfiguration : gkStationConfigurationList1) {
                                        if (gkStationConfiguration.getId().longValue() == stationConfiguration.getId().longValue()) {
                                            gkStationConfiguration.setTempToUse(stationConfiguration.getTempToUse());
                                            gkStationConfiguration.setDisplayName(stationConfiguration.getDisplayName());
                                        }
                                    }
                                }
                            }
                        }
                        for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
                            GkStationConfigurationVo gkStationConfigurationVo = new GkStationConfigurationVo();
                            BeanUtils.copyProperties(gkStationConfiguration, gkStationConfigurationVo);
                            gkStationConfigurationVos.add(gkStationConfigurationVo);
                        }
                        for (GkStationConfigurationVo gkStationConfigurationVo : gkStationConfigurationVos) {

                            if (gkStationConfigurationVo.getStationId().equals(gkStation.getId())) {
                                int flag = 1;
                                // 同一个工位下
                                for (GkStationConfigurationValues gkStationConfigurationValue : gkStationConfigurationValues) {
                                    if (gkStationConfigurationValue.getStationConfigurationId().equals(gkStationConfigurationVo.getId())) {
                                        if (gkStationConfigurationVo.getResultConversion() == 1) {
                                            String value = "";
                                            switch (gkStationConfigurationValue.getValue()) {
                                                case "0":
                                                case "0.0":
                                                    value = gkStationConfigurationVo.getZeroValue();
                                                    break;
                                                case "1":
                                                case "1.0":
                                                    value = gkStationConfigurationVo.getOneValue();
                                                    break;
                                                case "2":
                                                case "2.0":
                                                    value = gkStationConfigurationVo.getTwoValue();
                                                    break;
                                                case "3":
                                                case "3.0":
                                                    value = gkStationConfigurationVo.getThreeValue();
                                                    break;
                                                case "4":
                                                case "4.0":
                                                    value = gkStationConfigurationVo.getFourValue();
                                                    break;
                                                case "5":
                                                case "5.0":
                                                    value = gkStationConfigurationVo.getFiveValue();
                                                    break;
                                            }
                                            if (!StringUtils.isEmpty(value)) {
                                                gkStationConfigurationValue.setValue(value);
                                            }
                                        }
                                        // 同一个变量
                                        gkStationConfigurationVo.setSaveValue(gkStationConfigurationValue.getValue());
                                    }
                                }
                                if (gkStationConfigurationVo.getTempToUse() != null && gkStationConfigurationVo.getTempToUse() == 1) {
                                    gkStationConfigurationVoList.add(gkStationConfigurationVo);
                                } else if (CollUtil.isEmpty(tempIds)) {
                                    gkStationConfigurationVoList.add(gkStationConfigurationVo);
                                }
                            }

                        }
                        dataTraceabilityInfosVo.setGkStationConfigurationVoList(gkStationConfigurationVoList);
                        dataTraceabilityInfosVos.add(dataTraceabilityInfosVo);
                    }
                    map.put("dataTraceabilityInfos", dataTraceabilityInfosVos);
                }
                mapList.add(map);
            }
            return AjaxResult.success(mapList);
        }
        return AjaxResult.success();
    }


    @Override
    public void exportProductInfos(Long[] ids, GkStationConfigurationConnect gkStationConfigurationConnect, HttpServletResponse response) {
        if (gkStationConfigurationConnect == null) {
            return;
        }
        String tempDir = "临时文件" + LocalDate.now() + "/temDir/" + DateUtils.getTime().replace(" ", "").replace(":", "") + "/";
        List<GkStationConfigurationConnect> gkStationConfigurationConnects = new ArrayList<>();
        if (ids != null && ids.length > 0) {
            gkStationConfigurationConnects = gkStationConfigurationConnectMapper.selectInfoByIds(ids);
        } else if (StringUtils.isNotEmpty(gkStationConfigurationConnect.getBarCode())) {
            List<GkStationConfigurationValues> stationConfigurationValues = gkStationConfigurationValuesMapper.selectByValue(gkStationConfigurationConnect.getBarCode());
            if (CollUtil.isEmpty(stationConfigurationValues)){
                List<GkDataValueLog> gkDataValueLogList = new ArrayList<>();
                if (gkStationConfigurationConnect.getStartTime()!=null&&gkStationConfigurationConnect.getEndTime()!=null){
                    gkDataValueLogList = gkDataValueLogMapper.selectByTime(gkStationConfigurationConnect.getStartTime(),gkStationConfigurationConnect.getEndTime());
                }else {
                    // 近三个月的数据
                    gkDataValueLogList = gkDataValueLogMapper.selectLimit14();
                }
                if (CollUtil.isNotEmpty(gkDataValueLogList)) {
                    for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByValueAndByTableName(gkDataValueLog.getSurfaceName(), gkStationConfigurationConnect.getBarCode());
                        if (CollUtil.isNotEmpty(gkStationConfigurationValues)) {
                            stationConfigurationValues.addAll(gkStationConfigurationValues);
                        }
                    }
                }
            }
            if (CollUtil.isNotEmpty(stationConfigurationValues)) {
                List<Long> connectIds = stationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationConnectId).distinct().collect(Collectors.toList());
                if (gkStationConfigurationConnect.getStartTime() != null && gkStationConfigurationConnect.getEndTime() != null) {
                    List<DataTraceabilityVo> dataTraceabilityVos = gkStationConfigurationConnectMapper.selectInfoByIdsAndTime(connectIds.toArray(new Long[0]), gkStationConfigurationConnect);
                    for (DataTraceabilityVo dataTraceabilityVo : dataTraceabilityVos) {
                        GkStationConfigurationConnect configurationConnect = new GkStationConfigurationConnect();
                        BeanUtils.copyProperties(dataTraceabilityVo, configurationConnect, new String[]{"barCode"});
                        gkStationConfigurationConnects.add(configurationConnect);
                    }
                } else {
                    gkStationConfigurationConnects = gkStationConfigurationConnectMapper.selectInfoByIds(connectIds.toArray(new Long[0]));
                }
            }
        } else if (gkStationConfigurationConnect.getStartTime() != null && gkStationConfigurationConnect.getEndTime() != null) {
            gkStationConfigurationConnects = gkStationConfigurationConnectMapper.listReturnEntity(gkStationConfigurationConnect);
        }
        // 得到了需要导出的conn
        if (gkStationConfigurationConnects != null && gkStationConfigurationConnects.size() > 0) {
//            int allTotal = gkStationConfigurationConnects.size();
//            int size = 1;
//            int allLoop = allTotal / size;
            List<File> fileList = new ArrayList<>();
//            log.info("总数：{},总循环：{}",allTotal,allLoop);
            int allLoop = gkStationConfigurationConnects.size();
            for (int i = 0; i < allLoop; i++) {
                List<GkStationConfigurationConnect> gkStationConfigurationConnects1 = new ArrayList<>();
//                gkStationConfigurationConnects1 = gkStationConfigurationConnects.stream().skip(i*size).limit(size).collect(Collectors.toList());

//                List<Long> connectIds = gkStationConfigurationConnects1.stream().map(GkStationConfigurationConnect::getId).collect(Collectors.toList());
                // 需要导出的conn数据
                gkStationConfigurationConnects1.add(gkStationConfigurationConnects.get(i));
                List<GkStationConfigurationValues> exportValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectId(gkStationConfigurationConnects.get(i).getId());
                if (CollUtil.isEmpty(exportValues)){
                    exportValues = new ArrayList<>();
                    List<GkDataValueLog> gkDataValueLogList = new ArrayList<>();
                    if (gkStationConfigurationConnect.getStartTime()!=null&&gkStationConfigurationConnect.getEndTime()!=null){
                        gkDataValueLogList = gkDataValueLogMapper.selectByTime(gkStationConfigurationConnect.getStartTime(),gkStationConfigurationConnect.getEndTime());
                    }else {
                        // 近三个月的数据
                        gkDataValueLogList = gkDataValueLogMapper.selectLimit14();
                    }
                    if (CollUtil.isNotEmpty(gkDataValueLogList)) {
                        Date lastModifyAt = gkStationConfigurationConnects.get(i).getLastModifyAt();
                        for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                            if (lastModifyAt.compareTo(gkDataValueLog.getDataStartTime())>=0&& lastModifyAt.compareTo(gkDataValueLog.getDataEndTime())<=0) {
                                List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectIdByTableName(gkDataValueLog.getSurfaceName(), gkStationConfigurationConnects.get(i).getId());
                                if (CollUtil.isNotEmpty(gkStationConfigurationValues)) {
                                    exportValues.addAll(gkStationConfigurationValues);
                                }
                            }
                        }
                    }
                }
                // 找到模板产品对应的模板参数
                List<String> productNames = gkStationConfigurationConnects1.stream().map(GkStationConfigurationConnect::getProductName).collect(Collectors.toList());
                List<GkProduct> gkProducts = gkProductMapper.selectByNames(productNames);
                List<String> tempNames = gkProducts.stream().map(GkProduct::getTemplateName).collect(Collectors.toList());
                List<GkStationConfigureTemplate> gkStationConfigureTemplates = new ArrayList<>();
                List<Long> tempIds = new ArrayList<>();
                if (CollUtil.isNotEmpty(tempNames)) {
                    gkStationConfigureTemplates = gkStationConfigureTemplateMapper.selectInfoByNames(tempNames);
                    tempIds = gkStationConfigureTemplates.stream().map(GkStationConfigureTemplate::getId).collect(Collectors.toList());
                }
                Map<Long, List<GkStationConfigureTemplateInfo>> connIdTempInfoMap = new HashMap<>();
                if (CollUtil.isNotEmpty(tempIds)) {
                    List<GkStationConfigureTemplateInfo> gkStationConfigureTemplateInfos = gkStationConfigureTemplateInfoMapper.selectInfoByConfigureTemplateIds(tempIds);
                    Map<Long, List<GkStationConfigureTemplateInfo>> tempInfoByTempIdMap = gkStationConfigureTemplateInfos.parallelStream().collect(Collectors.groupingBy(GkStationConfigureTemplateInfo::getConfigureTemplateId));
                    for (GkStationConfigurationConnect stationConfigurationConnect : gkStationConfigurationConnects1) {
                        List<GkStationConfigureTemplateInfo> gkStationConfigureTemplateInfoList = new ArrayList<>();
                        for (GkProduct gkProduct : gkProducts) {
                            if (stationConfigurationConnect.getProductName() != null) {
                                if (stationConfigurationConnect.getProductName().equals(gkProduct.getName())) {
                                    for (GkStationConfigureTemplate gkStationConfigureTemplate : gkStationConfigureTemplates) {
                                        if (gkStationConfigureTemplate.getName().equals(gkProduct.getTemplateName())) {
                                            gkStationConfigureTemplateInfoList.addAll(tempInfoByTempIdMap.get(gkStationConfigureTemplate.getId()));
                                        }
                                    }
                                }
                            }
                        }
                        connIdTempInfoMap.put(stationConfigurationConnect.getId(), gkStationConfigureTemplateInfoList);
                    }
                }
                Map<Long, List<GkStationConfigurationValues>> connIdConfigValueMap = new HashMap<>();
                connIdConfigValueMap = exportValues.parallelStream().collect(Collectors.groupingBy(GkStationConfigurationValues::getStationConfigurationConnectId));

                List<GkStationConfiguration> gkStationConfigurationList = new ArrayList<>();
                List<Long> configIds = exportValues.stream().map(GkStationConfigurationValues::getStationConfigurationId).collect(Collectors.toList());
                // 导出的数据
                if (CollUtil.isNotEmpty(configIds)) {
                    gkStationConfigurationList = gkStationConfigurationMapper.selectInfoByIds(configIds);
                }
//                List<Long> stationIds = gkStationConfigurationList.stream().map(GkStationConfiguration::getStationId).distinct().collect(Collectors.toList());
                List<GkStation> gkStations = gkStationMapper.allInfo();
                List<GkProductionLineVo> productionLineVos = gkProductionLineMapper.list(null);


                for (GkStationConfigurationConnect stationConfigurationConnect : gkStationConfigurationConnects1) {
                    List<Map<String, Object>> mapList = new ArrayList<>();
                    // 需要导出的值
                    List<GkStationConfigurationValues> gkStationConfigurationValuesList = connIdConfigValueMap.get(stationConfigurationConnect.getId());
                    // 需要导出的模板信息
                    List<GkStationConfigureTemplateInfo> gkStationConfigureTemplateInfos = connIdTempInfoMap.get(stationConfigurationConnect.getId());
                    String productName = stationConfigurationConnect.getProductName();
                    String assemblyCode = stationConfigurationConnect.getAssemblyCode();
                    String traceabilityCode = stationConfigurationConnect.getTraceabilityCode();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String fileName = new String();
                    Date createAt = stationConfigurationConnect.getCreateAt();
                    String format = simpleDateFormat.format(createAt);
                    fileName = (StringUtils.isNotEmpty(productName) ? productName + "-" : "") + (StringUtils.isNotEmpty(assemblyCode) ? assemblyCode + "_" : "") + (StringUtils.isNotEmpty(traceabilityCode) ? traceabilityCode + "_" : "") + (format);
                    for (GkStation gkStation : gkStations) {
                        Map<String, Object> map = new HashMap<>();
                        // 每个工位一张sheet
                        List<GkStationConfiguration> gkStationConfigurationVoList = new ArrayList<>();
                        for (GkStationConfiguration gkStationConfiguration : gkStationConfigurationList) {
                            if (CollUtil.isNotEmpty(gkStationConfigureTemplateInfos)) {
                                for (GkStationConfigureTemplateInfo gkStationConfigureTemplateInfo : gkStationConfigureTemplateInfos) {
                                    if (gkStationConfiguration.getId().longValue() == gkStationConfigureTemplateInfo.getStationConfigurationId().longValue()) {
                                        gkStationConfiguration.setTempToUse(gkStationConfigureTemplateInfo.getTempToUse());
                                        if (StringUtils.isNotEmpty(gkStationConfigureTemplateInfo.getTempConfigName())) {
                                            gkStationConfiguration.setDisplayName(gkStationConfigureTemplateInfo.getTempConfigName());
                                        }
                                        break;
                                    }
                                }
                            } else {
                                gkStationConfiguration.setTempToUse(1);
                            }
                            if (gkStation.getId().longValue() == gkStationConfiguration.getStationId().longValue()) {
                                if (CollUtil.isNotEmpty(gkStationConfigurationValuesList)) {
                                    for (GkStationConfigurationValues stationConfigurationValues : gkStationConfigurationValuesList) {
                                        if (stationConfigurationValues.getStationConfigurationId().longValue() == gkStationConfiguration.getId().longValue() && gkStationConfiguration.getTempToUse() != null && gkStationConfiguration.getTempToUse() == 1) {
                                            if (gkStationConfiguration.getResultConversion() == 1) {
                                                String value = "";
                                                switch (stationConfigurationValues.getValue()) {
                                                    case "0":
                                                        ;
                                                    case "0.0":
                                                        value = gkStationConfiguration.getZeroValue();
                                                        break;
                                                    case "1":
                                                    case "1.0":
                                                        value = gkStationConfiguration.getOneValue();
                                                        break;
                                                    case "2":
                                                    case "2.0":
                                                        value = gkStationConfiguration.getTwoValue();
                                                        break;
                                                    case "3":
                                                    case "3.0":
                                                        value = gkStationConfiguration.getThreeValue();
                                                        break;
                                                    case "4":
                                                    case "4.0":
                                                        value = gkStationConfiguration.getFourValue();
                                                        break;
                                                    case "5":
                                                    case "5.0":
                                                        value = gkStationConfiguration.getFiveValue();
                                                        break;
                                                }
                                                if (!StringUtils.isEmpty(value)) {
                                                    stationConfigurationValues.setValue(value);
                                                }
                                            }
                                            gkStationConfiguration.setSaveValue(stationConfigurationValues.getValue());
                                            gkStationConfigurationVoList.add(gkStationConfiguration);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (CollUtil.isNotEmpty(gkStationConfigurationList)) {
                            String sheetName = new String();
                            for (GkProductionLineVo productionLineVo : productionLineVos) {
                                if (gkStation.getProductionLineId().longValue() == productionLineVo.getId().longValue()) {
                                    sheetName = productionLineVo.getName() + "#" + gkStation.getName();
                                    sheetName = sheetName.replace("/", "").replace("\\", "");
                                }
                            }
                            map.put("sheetName", sheetName);
                            map.put("sheetData", gkStationConfigurationVoList);
                            if (gkStationConfigurationVoList.size() > 0) {
                                mapList.add(map);
                            }
                        }
                    }
                    if (mapList.size() > 0) {
                        String filePath = exportSheets(mapList, fileName, tempDir);
                        fileList.add(new File(filePath));
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    log.error(e.getMessage());
                }
            }
            if (CollUtil.isNotEmpty(fileList)) {
                fileList = fileList.stream().distinct().collect(Collectors.toList());
                try {
                    response.setHeader("content-type", "application/octet-stream");
                    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "DataTranceability.zip");
                    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                    ZipUtils.downloadZipForFiles(response.getOutputStream(), fileList);
                    FileDownloadUtils.deleteDir(new File(tempDir).getParentFile().getParentFile());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    public void exportByStationId(DataTraceabilityVo dataTraceabilityVo, HttpServletResponse response) {
        List<GkStation> gkStations = gkStationMapper.selectInfosByIds(Collections.singletonList(dataTraceabilityVo.getStationId()));
        GkStationConfigurationConnect configurationConnect = gkStationConfigurationConnectMapper.selectInfoById(dataTraceabilityVo.getId());
        if (configurationConnect == null) {
            return;
        }
        GkProduct gkProduct = new GkProduct();
        gkProduct.setName(configurationConnect.getProductName());
        List<GkProduct> gkProducts = gkProductMapper.list(gkProduct);
        List<String> tempNames = gkProducts.stream().map(GkProduct::getTemplateName).collect(Collectors.toList());
        List<GkStationConfigureTemplate> gkStationConfigureTemplates = new ArrayList<>();
        List<Long> tempIds = new ArrayList<>();
        if (CollUtil.isNotEmpty(tempNames)) {
            gkStationConfigureTemplates = gkStationConfigureTemplateMapper.selectInfoByNames(tempNames);
            tempIds = gkStationConfigureTemplates.stream().map(GkStationConfigureTemplate::getId).collect(Collectors.toList());
        }
        List<GkStationConfigureTemplateInfo> gkStationConfigureTemplateInfos = new ArrayList<>();
        if (CollUtil.isNotEmpty(tempIds)) {
            gkStationConfigureTemplateInfos = gkStationConfigureTemplateInfoMapper.selectInfoByConfigureTemplateIds(tempIds);
        }

        List<GkStationConfigurationVo> gkStationConfigurationVos = gkStationConfigurationMapper.selectInfoByType2StationIds(Collections.singletonList(dataTraceabilityVo.getStationId()));
        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectIds(Collections.singletonList(configurationConnect.getId()));
        if (CollUtil.isEmpty(gkStationConfigurationValues)){
            gkStationConfigurationValues = new ArrayList<>();
            List<GkDataValueLog> gkDataValueLogList = gkDataValueLogMapper.selectAll();
            if (CollUtil.isNotEmpty(gkDataValueLogList)) {
                Date lastModifyAt = configurationConnect.getLastModifyAt();
                for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                    if (lastModifyAt.compareTo(gkDataValueLog.getDataStartTime())>=0&& lastModifyAt.compareTo(gkDataValueLog.getDataEndTime())<=0) {
                        List<GkStationConfigurationValues> gkStationConfigurationValuesList = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectIdByTableName(gkDataValueLog.getSurfaceName(), configurationConnect.getId());
                        if (CollUtil.isNotEmpty(gkStationConfigurationValuesList)) {
                            gkStationConfigurationValues.addAll(gkStationConfigurationValuesList);
                        }
                    }
                }
            }
        }
        String sheetName = null;
        if (CollUtil.isNotEmpty(gkStationConfigureTemplateInfos)) {
            for (GkStationConfigurationVo gkStationConfigurationVo : gkStationConfigurationVos) {
                for (GkStationConfigureTemplateInfo gkStationConfigureTemplateInfo : gkStationConfigureTemplateInfos) {
                    if (gkStationConfigurationVo.getId().longValue() == gkStationConfigureTemplateInfo.getStationConfigurationId().longValue()) {
                        gkStationConfigurationVo.setTempToUse(gkStationConfigureTemplateInfo.getTempToUse());
                        if (StringUtils.isNotEmpty(gkStationConfigureTemplateInfo.getTempConfigName())){
                            gkStationConfigurationVo.setDisplayName(gkStationConfigureTemplateInfo.getTempConfigName());
                        }
                    }
                }
            }
        }
        // todo 添加过滤注释
//        else {
//            for (GkStationConfigurationVo gkStationConfigurationVo : gkStationConfigurationVos) {
//                gkStationConfigurationVo.setTempToUse(1);
//            }
//        }
        for (GkStation gkStation : gkStations) {
            for (GkStationConfigurationVo gkStationConfigurationVo : gkStationConfigurationVos) {
                if (gkStation.getId().equals(gkStationConfigurationVo.getStationId())) {
                    int flag = 0;
                    for (GkStationConfigurationValues gkStationConfigurationValue : gkStationConfigurationValues) {
                        if (gkStationConfigurationValue.getStationConfigurationId().equals(gkStationConfigurationVo.getId())) {
                            if (gkStationConfigurationVo.getResultConversion() == 1) {
                                String s = gkStationConfigurationValue.getValue().toString();
                                String value = "";
                                switch (s) {
                                    case "0":
                                        ;
                                    case "0.0":
                                        value = gkStationConfigurationVo.getZeroValue();
                                        break;
                                    case "1":
                                    case "1.0":
                                        value = gkStationConfigurationVo.getOneValue();
                                        break;
                                    case "2":
                                    case "2.0":
                                        value = gkStationConfigurationVo.getTwoValue();
                                        break;
                                    case "3":
                                    case "3.0":
                                        value = gkStationConfigurationVo.getThreeValue();
                                        break;
                                    case "4":
                                    case "4.0":
                                        value = gkStationConfigurationVo.getFourValue();
                                        break;
                                    case "5":
                                    case "5.0":
                                        value = gkStationConfigurationVo.getFiveValue();
                                        break;
                                }
                                if (!StringUtils.isEmpty(value)) {
                                    gkStationConfigurationValue.setValue(value);
                                }
                            }
                            gkStationConfigurationVo.setDefaultValue(gkStationConfigurationValue.getValue());
                            flag = 1;
                        }
                    }
                    if (flag == 0){
                        gkStationConfigurationVo.setDefaultValue(null);
                    }
                }
            }
            sheetName = gkStation.getName();
        }
        Workbook workbook = null;
        if (gkStationConfigurationVos.size() <= 0) {
            return;
        }
        try {
            gkStationConfigurationVos = gkStationConfigurationVos.stream().filter(gkStationConfigurationVo -> gkStationConfigurationVo.getTempToUse() != null && gkStationConfigurationVo.getTempToUse() == 1).collect(Collectors.toList());
            if (CollUtil.isEmpty(gkStationConfigurationVos)) {
                return;
            }
            gkStationConfigurationVos = gkStationConfigurationVos.stream().sorted((s1, s2) -> s1.getVariableName().toLowerCase().contains("barcode") ? -1 : s2.getVariableName().toLowerCase().contains("barcode") ? 1 : 0).collect(Collectors.toList());
            ExportParams exportParams = new ExportParams();
            sheetName = sheetName.replace("/", "").replace("\\", "");
            exportParams.setSheetName(sheetName);
            Map<String, Object> map = new HashMap<>();
            map.put("title", exportParams);
            map.put("entity", GkStationConfigurationVo.class);
            map.put("data", gkStationConfigurationVos);
            List<Map<String, Object>> list = new ArrayList<>();
            list.add(map);
            workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);

            String filename = "工位数据";

            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
            response.setHeader("Cache-Control", "No-cache");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    //强行关流
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void exportDataById(Long id, HttpServletResponse response) {
        Workbook workbook = null;
        // 得到id所在的连接表值
        GkStationConfigurationConnect stationConfigurationConnect = gkStationConfigurationConnectMapper.selectInfoById(id);
        if (stationConfigurationConnect == null) {
            return;
        }

        // 根据模板名称查询到相对应的模板信息
        String productName = stationConfigurationConnect.getProductName();
        GkProduct gkProduct = new GkProduct();
        gkProduct.setName(productName);
        List<GkProduct> list = gkProductMapper.list(gkProduct);
        List<String> tempNames = list.stream().map(GkProduct::getTemplateName).collect(Collectors.toList());
        List<GkStationConfigureTemplate> gkStationConfigureTemplates = new ArrayList<>();
        // todo 添加注释，更新版产品名称不能使用
//        if (CollUtil.isNotEmpty(tempNames)) {
            gkStationConfigureTemplates = gkStationConfigureTemplateMapper.selectInfoByNames(tempNames);
//        }
        List<Long> tempIds = gkStationConfigureTemplates.stream().map(GkStationConfigureTemplate::getId).collect(Collectors.toList());
        Map<Long, List<GkStationConfiguration>> configByStationIdMap = new HashMap<>();
        if (CollUtil.isNotEmpty(tempIds)) {
            List<GkStationConfigureTemplateInfo> gkStationConfigureTemplateInfos = gkStationConfigureTemplateInfoMapper.selectInfoByConfigureTemplateIds(tempIds);
            List<Long> configIds = gkStationConfigureTemplateInfos.stream().map(GkStationConfigureTemplateInfo::getStationConfigurationId).collect(Collectors.toList());
            List<GkStationConfiguration> configInfos = gkStationConfigurationMapper.selectInfoByIds(configIds);
            for (GkStationConfiguration configInfo : configInfos) {
                for (GkStationConfigureTemplateInfo gkStationConfigureTemplateInfo : gkStationConfigureTemplateInfos) {
                    if (gkStationConfigureTemplateInfo.getStationConfigurationId().longValue() == configInfo.getId().longValue()) {
                        configInfo.setTempToUse(gkStationConfigureTemplateInfo.getTempToUse());
                        if (StringUtils.isNotEmpty(gkStationConfigureTemplateInfo.getTempConfigName())){
                            configInfo.setDisplayName(gkStationConfigureTemplateInfo.getTempConfigName());
                        }
                    }
                }
            }
            configInfos = configInfos.stream().filter(configInfo -> configInfo.getType() == 2).collect(Collectors.toList());
            configByStationIdMap = configInfos.parallelStream().collect(Collectors.groupingBy(GkStationConfiguration::getStationId));
        }

        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectId(id);
        if (CollUtil.isEmpty(gkStationConfigurationValues)){
            gkStationConfigurationValues = new ArrayList<>();
            List<GkDataValueLog> gkDataValueLogList = gkDataValueLogMapper.selectAll();
            if (CollUtil.isNotEmpty(gkDataValueLogList)) {
                Date lastModifyAt = stationConfigurationConnect.getLastModifyAt();
                for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                    if (lastModifyAt.compareTo(gkDataValueLog.getDataStartTime())>=0&& lastModifyAt.compareTo(gkDataValueLog.getDataEndTime())<=0) {
                        List<GkStationConfigurationValues> gkStationConfigurationValuesList = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectIdAndTableName(gkDataValueLog.getSurfaceName(), id);
                        if (CollUtil.isNotEmpty(gkStationConfigurationValuesList)) {
                            gkStationConfigurationValues.addAll(gkStationConfigurationValuesList);
                        }
                    }
                }
            }
        }
        // 工位事件集合，得到所需要读取的工位
        List<Long> configId = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationId).distinct().collect(Collectors.toList());
        List<GkStationConfiguration> gkStationConfigurationList = gkStationConfigurationMapper.selectInfoByIds(configId);
        List<Long> stationIds = gkStationConfigurationList.stream().map(GkStationConfiguration::getStationId).distinct().collect(Collectors.toList());
        List<GkStation> stationList = gkStationMapper.selectInfosByIds(stationIds);
        List<Long> productionLineIds = stationList.stream().map(GkStation::getProductionLineId).collect(Collectors.toList());
        List<GkProductionLineVo> gkProductionLineVos = gkProductionLineMapper.selectInfoByIds(productionLineIds);

        Map<Long, List<GkStation>> stationByProductLineMap = stationList.parallelStream().collect(Collectors.groupingBy(GkStation::getProductionLineId));
        Map<Long, List<GkStationConfiguration>> configByStationMap = gkStationConfigurationList.parallelStream().collect(Collectors.groupingBy(GkStationConfiguration::getStationId));

        List<Map<String, Object>> mapList = new ArrayList<>();
        for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
            List<GkStation> gkStations = stationByProductLineMap.get(gkProductionLineVo.getId());
            if (CollUtil.isNotEmpty(gkStations)) {
                for (GkStation gkStation : gkStations) {
                    List<ExportDataTraceabilityVo> exportDataTraceabilityVos = new ArrayList<>();
                    List<GkStationConfigurationVo> gkStationConfigurationVos = new ArrayList<>();
                    List<GkStationConfiguration> gkStationConfigurations = configByStationMap.get(gkStation.getId());
                    if (CollUtil.isNotEmpty(configByStationIdMap)) {
                        List<GkStationConfiguration> gkStationConfigurationList1 = configByStationIdMap.get(gkStation.getId());
                        if (CollUtil.isNotEmpty(gkStationConfigurationList1)) {
                            // 把模板使用赋值给相对应的工位信息上
                            for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
                                for (GkStationConfiguration stationConfiguration : gkStationConfigurationList1) {
                                    if (gkStationConfiguration.getId().longValue() == stationConfiguration.getId().longValue()) {
                                        gkStationConfiguration.setTempToUse(stationConfiguration.getTempToUse());
                                        gkStationConfiguration.setDisplayName(stationConfiguration.getDisplayName());
                                    }
                                }
                            }
                        }
                    }
                    for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
                        GkStationConfigurationVo gkStationConfigurationVo = new GkStationConfigurationVo();
                        BeanUtils.copyProperties(gkStationConfiguration, gkStationConfigurationVo);
                        gkStationConfigurationVos.add(gkStationConfigurationVo);
                    }
                    for (GkStationConfigurationVo gkStationConfigurationVo : gkStationConfigurationVos) {
                        if (gkStationConfigurationVo.getTempToUse() != null && gkStationConfigurationVo.getTempToUse() == 1 || CollUtil.isEmpty(tempIds)) {
                            if (gkStationConfigurationVo.getStationId().equals(gkStation.getId())) {
                                // 同一个工位下
                                for (GkStationConfigurationValues gkStationConfigurationValue : gkStationConfigurationValues) {
                                    if (gkStationConfigurationValue.getStationConfigurationId().equals(gkStationConfigurationVo.getId())) {
                                        if (gkStationConfigurationVo.getResultConversion() == 1) {
                                            String value = "";
                                            switch (gkStationConfigurationValue.getValue()) {
                                                case "0":
                                                case "0.0":
                                                    value = gkStationConfigurationVo.getZeroValue();
                                                    break;
                                                case "1":
                                                case "1.0":
                                                    value = gkStationConfigurationVo.getOneValue();
                                                    break;
                                                case "2":
                                                case "2.0":
                                                    value = gkStationConfigurationVo.getTwoValue();
                                                    break;
                                                case "3":
                                                case "3.0":
                                                    value = gkStationConfigurationVo.getThreeValue();
                                                    break;
                                                case "4":
                                                case "4.0":
                                                    value = gkStationConfigurationVo.getFourValue();
                                                    break;
                                                case "5":
                                                case "5.0":
                                                    value = gkStationConfigurationVo.getFiveValue();
                                                    break;
                                            }
                                            if (!StringUtils.isEmpty(value)) {
                                                gkStationConfigurationValue.setValue(value);
                                            }
                                        }
                                        // 同一个变量
                                        gkStationConfigurationVo.setSaveValue(gkStationConfigurationValue.getValue());
                                    }
                                }
                                ExportDataTraceabilityVo exportDataTraceabilityVo = new ExportDataTraceabilityVo();
                                exportDataTraceabilityVo.setDataType(gkStationConfigurationVo.getDataType());
                                exportDataTraceabilityVo.setStationName(gkStation.getName());
                                exportDataTraceabilityVo.setVariableComments(gkStationConfigurationVo.getVariableComments());
                                exportDataTraceabilityVo.setVariableName(gkStationConfigurationVo.getVariableName());
                                exportDataTraceabilityVo.setNameSpaces(gkStationConfigurationVo.getNameSpaces());
                                exportDataTraceabilityVo.setVariableRemarks(gkStationConfigurationVo.getVariableRemarks());
                                exportDataTraceabilityVo.setDisplayName(gkStationConfigurationVo.getDisplayName());
                                exportDataTraceabilityVo.setSaveValue(gkStationConfigurationVo.getSaveValue());
                                exportDataTraceabilityVo.setVariableAddress(gkStationConfigurationVo.getVariableAddress());
                                exportDataTraceabilityVos.add(exportDataTraceabilityVo);
                            }
                        }
                    }
                    if (exportDataTraceabilityVos != null && exportDataTraceabilityVos.size() > 0) {
                        exportDataTraceabilityVos = exportDataTraceabilityVos.stream().sorted((s1, s2) -> s1.getVariableName().toLowerCase().contains("barcode") ? -1 : s2.getVariableName().toLowerCase().contains("barcode") ? 1 : 0).collect(Collectors.toList());
                        ExportParams exportParams = new ExportParams();
                        String sheetName = gkProductionLineVo.getName() + "-" + gkStation.getName();
                        sheetName = sheetName.replace("/", "").replace("\\", "");
                        exportParams.setSheetName(sheetName);

                        Map<String, Object> map = new HashMap<>();
                        map.put("title", exportParams);
                        map.put("entity", ExportDataTraceabilityVo.class);
                        map.put("data", exportDataTraceabilityVos);
                        mapList.add(map);
                    }
                }
            }
        }
        try {
            workbook = ExcelExportUtil.exportExcel(mapList, ExcelType.HSSF);
            String assemblyCode = stationConfigurationConnect.getAssemblyCode();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String fileName = new String();
            Date createAt = stationConfigurationConnect.getCreateAt();
            String traceabilityCode = stationConfigurationConnect.getTraceabilityCode();
            String format = simpleDateFormat.format(createAt.getTime());
            fileName = (StringUtils.isNotEmpty(productName)?productName+"-":"")+(StringUtils.isNotEmpty(assemblyCode)?assemblyCode+"_":"")+(StringUtils.isNotEmpty(traceabilityCode)?traceabilityCode+"_":"")+(format);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
            response.setHeader("Cache-Control", "No-cache");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (workbook != null) {
                try {
                    //强行关流
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void test(Long connectId, Integer start, Integer end, String tableName) {
        // 找到保存信息值，进行复制
//        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectId(connectId);
//        List<GkStationConfigurationValues> gkStationConfigurationValuesList1 = new ArrayList<>();
//        Date date1 = new Date();
//        Map<Long, List<GkStationConfigurationValues>> collect = gkStationConfigurationValues.parallelStream().collect(Collectors.groupingBy(GkStationConfigurationValues::getStationConfigurationId));
//        if (CollUtil.isNotEmpty(collect)){
//            for (Long aLong : collect.keySet()) {
//                List<GkStationConfigurationValues> gkStationConfigurationValuesList2 = collect.get(aLong);
//                if (CollUtil.isNotEmpty(gkStationConfigurationValuesList2)) {
//                    GkStationConfigurationValues stationConfigurationValues = gkStationConfigurationValuesList2.stream().max(Comparator.comparing(GkStationConfigurationValues::getId)).get();
//                    gkStationConfigurationValuesList1.add(stationConfigurationValues);
//                    List<Long> deleteOldValueIds = gkStationConfigurationValuesList2.stream().filter(gkStationConfigurationValues1 -> gkStationConfigurationValues1.getId().longValue() != stationConfigurationValues.getId().longValue()).map(GkStationConfigurationValues::getId).collect(Collectors.toList());
//                    if (CollUtil.isNotEmpty(deleteOldValueIds)) {
//                        int row = gkStationConfigurationValuesMapper.deleteByIds(deleteOldValueIds);
//                    }
//                }
//            }
//        }
//        for (int i = start; i < end; i++) {
//            GkStationConfigurationConnect gkStationConfigurationConnect = new GkStationConfigurationConnect();
//            gkStationConfigurationConnect.setId(((long) i));
//            Date date = new Date();
//            Date date1 = DateUtils.addWeeks(date, -2);
//            gkStationConfigurationConnect.setProductName("AE86");
//            gkStationConfigurationConnect.setProductId(47l);
//            gkStationConfigurationConnect.setProductLineName("GK22020总成装配线");
//            int i1 = RandomUtil.randomInt();
//            gkStationConfigurationConnect.setAssemblyCode("sdasdasd"+i);
//            gkStationConfigurationConnect.setTraceabilityCode("gfwefwef");
//            gkStationConfigurationConnect.setModels("正常生产");
//            gkStationConfigurationConnect.setResult(1);
//            gkStationConfigurationConnect.setIsOk(1);
//            gkStationConfigurationConnect.setCreateAt(date1);
//            gkStationConfigurationConnect.setLastModifyAt(date1);
//            gkStationConfigurationConnect.setStationId(126l);
//            gkStationConfigurationConnectMapper.addAllInfo(gkStationConfigurationConnect);
//        }
//        List<GkDataValueLog> gkDataValueLogList = gkDataValueLogMapper.selectAll();
//        for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
//            int row = gkDataValueLogMapper.deleteLimit150W(gkDataValueLog.getSurfaceName());
//        }
        List<GkStationConfigurationValues> configValueAndConfigConnectVos = gkStationConfigurationValuesMapper.selectAll();
        for (int i = start; i < end; i++) {
            for (GkStationConfigurationValues configValueAndConfigConnectVo : configValueAndConfigConnectVos) {
                configValueAndConfigConnectVo.setStationConfigurationConnectId(((long) i));
            }
            gkDataValueLogMapper.saveListValuesNoId(tableName,configValueAndConfigConnectVos);
        }
        System.out.println("aa");
    }

    public String exportSheets(List<Map<String, Object>> mapList, String fileName, String tempDir) {
        Workbook workbook = null;
        try {
            List<Map<String, Object>> maps = new ArrayList<>();
            for (Map<String, Object> map : mapList) {
                ExportParams exportParams = new ExportParams();
                Object sheetName = map.get("sheetName");
                Object sheetData = map.get("sheetData");
                exportParams.setSheetName(sheetName.toString());
                List<GkStationConfiguration> gkStationConfigurationVos = JSON.parseArray(JSON.toJSONString(sheetData), GkStationConfiguration.class);

                List<ExportDataTraceabilityVo> list = new ArrayList<>();
                if (CollUtil.isNotEmpty(gkStationConfigurationVos)) {
                    for (GkStationConfiguration gkStationConfigurationVo : gkStationConfigurationVos) {
                        ExportDataTraceabilityVo exportDataTraceabilityVo = new ExportDataTraceabilityVo();
                        exportDataTraceabilityVo.setDataType(gkStationConfigurationVo.getDataType());
                        exportDataTraceabilityVo.setDisplayName(gkStationConfigurationVo.getDisplayName());
                        exportDataTraceabilityVo.setSaveValue(gkStationConfigurationVo.getSaveValue());
                        exportDataTraceabilityVo.setNameSpaces(gkStationConfigurationVo.getNameSpaces());
                        exportDataTraceabilityVo.setVariableAddress(gkStationConfigurationVo.getVariableAddress());
                        exportDataTraceabilityVo.setVariableName(gkStationConfigurationVo.getVariableName());
                        exportDataTraceabilityVo.setVariableRemarks(gkStationConfigurationVo.getVariableRemarks());
                        exportDataTraceabilityVo.setVariableComments(gkStationConfigurationVo.getVariableComments());
                        list.add(exportDataTraceabilityVo);
                    }
                }
                list = list.stream().sorted((s1, s2) -> s1.getVariableName().toLowerCase().contains("barcode") ? -1 : s2.getVariableName().toLowerCase().contains("barcode") ? 1 : 0).collect(Collectors.toList());
                Map<String, Object> exportMap = new HashMap<>();
                exportMap.put("title", exportParams);
                exportMap.put("entity", ExportDataTraceabilityVo.class);
                exportMap.put("data", list);
                maps.add(exportMap);
            }

            FileDownloadUtils.createFile(tempDir);
            File file = new File(tempDir+fileName+".xls");
//            File tempFile = File.createTempFile(fileName, ".xls", file);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
//            response.setHeader("content-type", "application/octet-stream");
//            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + "DataTranceability.xls");
//            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            workbook = ExcelExportUtil.exportExcel(maps, ExcelType.HSSF);
            workbook.write(fileOutputStream);
            fileOutputStream.close();
            return file.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    //强行关流
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
